package me.zhengjie.modules.system.service;

import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * @author cjl
 * @className SqlUtilService
 * @description: TODO
 * @company: 上海明我信息技术有限公司
 * @date 2023年06月01日
 * @version: 1.0
 */
@Component
public class SqlUtilService {

    @Autowired
    private  EntityManager entityManager;

    public Map<String, Object> listBySQL(String sql) {

        Query query = entityManager.createNativeQuery(sql);

        query.unwrap(org.hibernate.SQLQuery.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

        List<Map<String, Object>> resultList = query.getResultList();
        return resultList.get(0);
    }

    public Object updateSql(String sql) {
        Query query = entityManager.createQuery(sql);
        int i = query.executeUpdate();
        return i;
    }
}
